//
//  PersonPassViewController.m
//  GoldOperationClient
//
//  Created by yaxue wang on 16/8/8.
//  Copyright © 2016年 展鸿科技. All rights reserved.
//

#import "PersonPassViewController.h"
#import "TPKeyboardAvoidingTableView.h"
#import "AppDelegate.h"
@interface PersonPassViewController()<UITableViewDataSource,UITableViewDelegate>
{
    TPKeyboardAvoidingTableView *_table;
    CTextField *_oldPasswordTF;
    CTextField *_newPasswordTF;
    NSInteger _eyeClick;
    NSInteger _eyeClick1;


    
}

@end
@implementation PersonPassViewController
- (void)initView
{
    [super initView];
    [self setTitle:@"修改密码"];
    _table =[[TPKeyboardAvoidingTableView alloc]initWithFrame:CGRectMake(0, 64, WindowWidth,WindowHeight-64)];
    _table.sectionHeaderHeight =0;
    _table.delegate =self;
    _table.dataSource =self;
    _table.backgroundColor =BackColor;
    _table.separatorStyle =UITableViewCellSelectionStyleNone;
    if(WindowHeight ==480)
    {
        _table.scrollEnabled=YES;
    }else
    {
        _table.scrollEnabled=NO;
    }    [self.view addSubview:_table];
    
    
}
//去掉粘合性
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView ==_table)
    {
        CGFloat sectionHeaderHeight = 39;
        if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
            scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
        } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
            scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
        }
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        CTextField *textField =[[CTextField alloc]initWithFrame:CGRectMake(25, 0.5, WindowWidth-130, 47)];
        textField.font =F(12);
        textField.inputType=CTextFieldInputTypePassword;
        textField.tag =100;
        [cell.contentView addSubview:textField];
        UILabel *line =[[UILabel alloc]initWithFrame:CGRectMake(0,0, WindowWidth, 0.5)];
        line.tag =200;
        line.backgroundColor=LineColor;
        [cell.contentView addSubview:line];
        UILabel *line1 =[[UILabel alloc]initWithFrame:CGRectMake(0,48-0.5, WindowWidth, 0.5)];
        line1.tag =300;
        line1.backgroundColor=LineColor;
        [cell.contentView addSubview:line1];
        
        UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom];
        button.frame =CGRectMake(WindowWidth-86, 7.5, 86, 33);
        [button setImage:[UIImage imageNamed:@"Ico_register_noEye"] forState:UIControlStateNormal];
        button.tag=400;
        [cell.contentView addSubview:button];
        
    }
    CTextField *textField =[cell viewWithTag:100];
    UIButton *btn =[cell viewWithTag:400];
    [textField setPlaceholder:@[@"请输入原密码", @"请输入新密码",][indexPath.section]];
    textField.secureTextEntry=YES;
    if (indexPath.section==0) {
        
        _oldPasswordTF=textField;
        [_oldPasswordTF setInputType:CTextFieldInputTypePassword];
        [_oldPasswordTF setBk_shouldChangeCharactersInRangeWithReplacementStringBlock:^BOOL(UITextField *textField, NSRange range, NSString *str) {
            NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"] invertedSet];
            NSString *filtered = [[str componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
            return [str isEqualToString:filtered];
        }];
        
        [_oldPasswordTF setMinLength:8 msg:@"请输入8-20位原登录密码!"];
        [_oldPasswordTF setNullMsg:@"请输入原密码!"];
        [_oldPasswordTF setMaxLength:20 LengthChangeBlock:nil];
        [btn bk_addEventHandler:^(id sender) {
            _eyeClick1++;
            if (_eyeClick1%2==0) {
                _oldPasswordTF.secureTextEntry=YES;
                [btn setImage:[UIImage imageNamed:@"Ico_register_noEye"] forState:UIControlStateNormal];
                
            }else{
                _oldPasswordTF.secureTextEntry=NO;
                [btn setImage:[UIImage imageNamed:@"Ico_register_eye"] forState:UIControlStateNormal];
            }
        } forControlEvents:UIControlEventTouchUpInside];
        [_oldPasswordTF bk_addEventHandler:^(id sender) {
            //移除表情
            NSString *newStr= [CClass disable_emoji:_oldPasswordTF.text];
            _oldPasswordTF.text=newStr;
            
            
        } forControlEvents:UIControlEventEditingChanged];
    
    }else if(indexPath.section==1)
    {
        _newPasswordTF=textField;
        [_newPasswordTF setBk_shouldChangeCharactersInRangeWithReplacementStringBlock:^BOOL(UITextField *textField, NSRange range, NSString *str) {
            NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"] invertedSet];
            NSString *filtered = [[str componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
            return [str isEqualToString:filtered];
        }];
        
        [_newPasswordTF setInputType:CTextFieldInputTypePassword];
        [_newPasswordTF setMinLength:8 msg:@"请输入8-20位新登录密码!"];
        [_newPasswordTF setNullMsg:@"请输入新密码!"];
        [_newPasswordTF setMaxLength:20 LengthChangeBlock:nil];
        [btn bk_addEventHandler:^(id sender) {
            _eyeClick++;
            if (_eyeClick%2==0) {
                _newPasswordTF.secureTextEntry=YES;
                [btn setImage:[UIImage imageNamed:@"Ico_register_noEye"] forState:UIControlStateNormal];
                
            }else{
                _newPasswordTF.secureTextEntry=NO;
                [btn setImage:[UIImage imageNamed:@"Ico_register_eye"] forState:UIControlStateNormal];
            }
        } forControlEvents:UIControlEventTouchUpInside];
        [_newPasswordTF bk_addEventHandler:^(id sender) {
            //移除表情
            NSString *newStr= [CClass disable_emoji:_newPasswordTF.text];
            _newPasswordTF.text=newStr;
            
            
        } forControlEvents:UIControlEventEditingChanged];
    }
    
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];


    return cell;

}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 48;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section==0) {
        return 50;
    }else
    {
        return 39;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section==1) {
        return 70;
    }else
    {
        return 0;
    }
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    UIView *view =[[UIView alloc]initWithFrame:CGRectMake(0, 0, WindowWidth, 39)];
    view.backgroundColor =BackColor;
    UILabel *lab =[[UILabel alloc]initWithFrame:CGRectMake(25, 0, 100, 39)];
    lab.font= F(14);
    lab.textColor=FontColor;
    [view addSubview:lab];
    if (section ==0) {
        view.frame =CGRectMake(0, 0, WindowWidth, 50);
        lab.frame =CGRectMake(25, 11, 100, 39);
    }
    NSArray *ar =[[NSArray alloc]initWithObjects:@"原密码",@"新密码", nil];
    lab.text =[ar objectAtIndex:section];
    return view;

    
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    
    UIView *view =[[UIView alloc]initWithFrame:CGRectMake(0, 0, WindowWidth, 70)];
    view.backgroundColor =BackColor;
    UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom];
    button.frame =CGRectMake(33, 40, WindowWidth-66, 35);
    [button setBackgroundColor:MainColor];
    button.layer.masksToBounds=YES;
    button.layer.cornerRadius=8;
    [button setTitle:@"确定" forState:UIControlStateNormal];
    button.titleLabel.font =F(15);
    [button bk_addEventHandler:^(id sender) {
      
        if (!_oldPasswordTF.verfiy) {
            return;
        }
        if (!_newPasswordTF.verfiy) {
            return;
        }
        if (_newPasswordTF.value.length>20) {
            [CClass showMessage:@"请输入8-20位新登录密码!"];
            return;
        }
        if (_oldPasswordTF.value.length>20) {
            [CClass showMessage:@"请输入8-20位原登录密码!"];
            return;
        }
        
        
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        [dic setObject:[CClass getCacheData:NSUD_UUID] forKey:@"uuid"];
        [dic setObject:[CClass base64:_oldPasswordTF.text] forKey:@"password"];
        [dic setObject:[CClass base64:_newPasswordTF.text] forKey:@"rawPasswd"];
        [self setIsLoading:YES];
        [CClass HttpRequestTask:YES withUrl:@"reviseaccount_revisePassword" withData:dic notifyError:YES onSuccess:^(BaseModel *model) {
            [CClass showMessage:@"修改成功，请重新登录"completion:^{

                //进行一个退出请求
                [CClass HttpRequestTask:YES withUrl:@"loginout_authtoken" withData:nil notifyError:YES onSuccess:^(BaseModel *model) {
                    [self setIsLoading:NO];
                    [CClass clearCacheData:@[NSUD_Token, NSUD_UUID,NSUD_state]];
                    [self login];
                  

                } onError:^(BaseModel *model) {
                    [self setIsLoading:NO];

                }];

            
            
            }];
            
        } onError:^(BaseModel *model) {
            
            //出错时候
            [self setIsLoading:NO];

        }];

        
    } forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:button];
    return view;

}
- (void) login{
    [self.navigationController popToRootViewControllerAnimated:NO];
    AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [delegate login];
    
}
@end
